/**
 * 
 */
package com.feonix.nio.test.mapperbytebuffer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @CopyRight (c)1993-2017
 * @Project common_practice
 * @Comments 请输入该类的描述
 * @NameSpace com.feonix.nio.test.mapperbytebuffer
 * @Author Feonixzw
 * @CreateDate 2018年3月12日
 */
public class MapperByteBuffer {

	public static void method4() {
		RandomAccessFile aFile = null;
		FileChannel fc = null;
		try {
			aFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\netty-4.1.22.Final.tar.bz2", "rw");
			fc = aFile.getChannel();

			long timeBegin = System.currentTimeMillis();
			ByteBuffer buff = ByteBuffer.allocate((int) aFile.length());
			buff.clear();
			fc.read(buff);
			// System.out.println((char)buff.get((int)(aFile.length()/2-1)));
			// System.out.println((char)buff.get((int)(aFile.length()/2)));
			// System.out.println((char)buff.get((int)(aFile.length()/2)+1));
			long timeEnd = System.currentTimeMillis();
			System.out.println("Read time: " + (timeEnd - timeBegin) + "ms");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (aFile != null) {
					aFile.close();
				}
				if (fc != null) {
					fc.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void method3() {
		RandomAccessFile aFile = null;
		FileChannel fc = null;
		try {
			aFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\netty-4.1.22.Final.tar.bz2", "rw");
			fc = aFile.getChannel();
			long timeBegin = System.currentTimeMillis();
			MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, aFile.length());
			long timeEnd = System.currentTimeMillis();
			System.out.println("Read time1: " + (timeEnd - timeBegin));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (aFile != null) {
					aFile.close();
				}
				if (fc != null) {
					fc.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		method3();
		method4();
	}

}
